package com.ruoyi.system.service.constructionLike.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.constructionLike.ConstructionLike;
import com.ruoyi.system.domain.constructionLike.ConstructionTags;
import com.ruoyi.system.mapper.constructionLike.ConstructionLikeMapper;
import com.ruoyi.system.mapper.constructionLike.ConstructionTagsMapper;
import com.ruoyi.system.service.constructionLike.ConstructionLikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 施工攻略点赞 Service业务层处理
 *
 * @author ruoyi
 * @date 2021-04-10
 */
@Service
public class ConstructionLikeServiceImpl implements ConstructionLikeService {
    @Autowired
    private ConstructionLikeMapper constructionLikeMapper;
    @Autowired
    private ConstructionTagsMapper constructionTagsMapper;

    /**
     * 查询施工攻略点赞 列表
     *
     * @param constructionLike 施工攻略点赞
     * @return 施工攻略点赞
     */
    @Override
    public ConstructionLike selectConstructionLikeList(ConstructionLike constructionLike)
    {
        return constructionLikeMapper.selectConstructionLikeList(constructionLike);
    }

    /**
     * 新增施工攻略点赞
     *
     * @param constructionLike 施工攻略点赞
     * @return 结果
     */
    @Override
    public int insertConstructionLike(ConstructionLike constructionLike) {
        //取出前端传过来的点赞状态编码（不将status设置为空则不会走修改方法）
        String status = constructionLike.getStatus();
        constructionLike.setStatus("");
        //查询是否对当前攻略点过赞
        ConstructionLike constructionLike1 = constructionLikeMapper.selectConstructionLikeList(constructionLike);
        if (constructionLike1 != null && !constructionLike1.equals("")){
            //如果对当前攻略点过赞则直接修改点赞状态
            constructionLike.setCreateTime(DateUtils.getNowDate());
            ConstructionTags constructionTags = new ConstructionTags();
            constructionTags.setId(Long.valueOf(constructionLike1.getTagsId()));
            constructionTags.setUserId(constructionLike.getUserId());
            constructionTags.setConstructionStrategyId(constructionLike.getConstructionStrategyId());
            constructionLike.setId(constructionLike1.getId());
            constructionLike.setStatus(status);
            int i = constructionTagsMapper.updateConstructionTags(constructionTags);
            if (i > 0){
                int i1 = constructionLikeMapper.updateConstructionLike(constructionLike);
                if (i1 > 0){
                    return 1;
                }
            }
        }else{
            //如果对当前攻略没有点过赞则直接添加一条点赞数据
            constructionLike.setCreateTime(DateUtils.getNowDate());
            ConstructionTags constructionTags = new ConstructionTags();
            constructionTags.setUserId(constructionLike.getUserId());
            constructionTags.setConstructionStrategyId(constructionLike.getConstructionStrategyId());
            constructionLike.setStatus(status);
            ConstructionTags constructionTags1 = constructionTagsMapper.selectConstructionTagsList(constructionTags);
            if (constructionTags1 != null && !constructionTags1.equals("")){
                constructionLike.setTagsId(Math.toIntExact(constructionTags1.getId()));
                int i1 = constructionLikeMapper.insertConstructionLike(constructionLike);
                if (i1 > 0){
                    return 1;
                }
            }else{
                int i = constructionTagsMapper.insertConstructionTags(constructionTags);
                constructionLike.setTagsId(Math.toIntExact(constructionTags.getId()));
                if (i > 0){
                    int i1 = constructionLikeMapper.insertConstructionLike(constructionLike);
                    if (i1 > 0){
                        return 1;
                    }
                }
            }
        }
        return 0;
    }

    /**
     * 修改施工攻略点赞
     *
     * @param constructionLike 施工攻略点赞
     * @return 结果
     */
    @Override
    public int updateConstructionLike(ConstructionLike constructionLike) {
        return 0;
    }

    @Override
    public int selectConstructionLikeCount() {
        return constructionLikeMapper.selectConstructionLikeCount();
    }
}
